import time
# import unittest
from common import open_browser
from common.base import Base, By
from page.login_page import LoginPage
from selenium.webdriver.support.ui import Select
from selenium import webdriver
# TODO 导入键盘类
from selenium.webdriver.common.keys import Keys

from selenium.webdriver.support.wait import WebDriverWait


# 打开浏览器,请求网址
class Buyshop(Base):
    # 在请求网址后，定位元素前，为整个driver设置隐式等待。保证在定位元素前，元素已经全部加载
    # driver.implicitly_wait(10)
    # driver = open_browser.open_browser()

    def find_shop(self):
        # 登录后点击首页才能跳到商品页面
        self.click((By.CLASS_NAME, 'cur'))

        # 定位商品
        self.click((By.XPATH, '/html/body/div[6]/div/div/div[2]/div[2]/div/a[1]'))

        # 点击立即购买
        # 定位购买按钮,点击
        time.sleep(2)
        self.click((By.CLASS_NAME, 'td1'))

    def update_shop(self):
        # 定位购买数量,增加一个商品
        time.sleep(2)

        self.find_element((By.CLASS_NAME, 'inputBg')).clear()  # 清空商品数量
        self.find_element((By.CLASS_NAME, 'inputBg')).send_keys('2')

        # 定位更新购物车,点击,购买数量才能更新成功
        time.sleep(2)
        self.click((By.NAME, 'submit'))

        # 定位去结算按钮,点击
        # time.sleep(2)
        # self.click((By.XPATH, '/html/body/div[6]/div[1]/table/tbody/tr/td[2]/a/img'))

    # 填写收货人信息
    def info_person(self):
        # 选择省
        time.sleep(5)
        province = self.find_element((By.NAME, 'province'))
        select = Select(province)
        time.sleep(2)
        select.select_by_index(2)  # 通过索引选取option

        # 选择市
        time.sleep(5)
        city = self.find_element((By.NAME, 'city'))
        select = Select(city)
        time.sleep(2)
        select.select_by_index(1)  # 通过索引选取option

        # 选择区
        time.sleep(5)
        district = self.find_element((By.NAME, 'district'))
        select = Select(district)
        time.sleep(2)
        select.select_by_index(1)  # 通过索引选取option

        # 收货人姓名
        self.find_element((By.NAME, 'consignee')).send_keys('wu')

        # 收货人地址
        self.find_element((By.NAME, 'address')).send_keys('address')

        # 电话
        self.find_element((By.NAME, 'tel')).send_keys('13111111111')

        # 点击配送按钮
        self.click((By.NAME, 'Submit'))

    # 删除商品
    def delete_shop(self):
        self.click((By.XPATH, '//*[@id="formCart"]/table[1]/tbody/tr[2]/td[7]/a[1]'))
        # 切换到弹窗点击确定
        time.sleep(2)
        alter = self.driver.switch_to.alert
        alter.accept()  # 确定

    # 清空购物车
    def delete_car(self):
        self.click((By.XPATH, '//input[@value="清空购物车"]'))

    # 选择配送方式 ,支付方式, 提交订单
    def delivery_way(self):
        self.click((By.XPATH, '//input[@value="5"]'))
        self.click((By.XPATH, '//input[@name="payment" and @value="2"]'))
        self.click((By.XPATH, '//*[@id="theForm"]/div[15]/div[2]/input[1]'))

    # 定位继续购物按钮,点击
    def goto_buy(self):
        self.click((By.XPATH, '/html/body/div[6]/div[1]/table/tbody/tr/td[1]/a/img'))
        self.click((By.XPATH, '/html/body/div[6]/div/div/div[2]/div[2]/div/a[2]'))  # 增加新商品
        self.click((By.CLASS_NAME, 'td1'))
        self.click((By.XPATH, '/html/body/div[6]/div[1]/table/tbody/tr/td[1]/a/img'))
        self.click((By.XPATH, '/html/body/div[6]/div/div/div[2]/div[2]/div/a[5]'))  # 增加新商品
        self.click((By.CLASS_NAME, 'td1'))

    # 去结算
    def buy(self):
        self.click((By.XPATH, '/html/body/div[6]/div[1]/table/tbody/tr/td[2]/a/img'))

    # 提交订单
    def all_money(self):
        self.click((By.XPATH, '//*[@id="theForm"]/div[15]/div[2]/input[1]'))


    # 首页进入购物车
    def car_shop(self):
        # time.sleep(2)
        self.click((By.CLASS_NAME, 'cart'))

    def login_ecshop(self):
        # 登录页面
        # self.LoginPage(driver).input_username('')
        LoginPage(self.driver).input_username('wuxn')
        LoginPage(self.driver).input_password('111111')
        LoginPage(self.driver).input_remember()
        LoginPage(self.driver).input_submit()


if __name__ == '__main__':
    driver = open_browser.open_browser()
    shops = Buyshop(driver)
    # shops.get('http://172.16.140.91/ecshop/user.php') #登录页面网址
    shops.get('http://172.16.140.47/ecshop/user.php')  # 自己部署的登录页面网址
    # shops.get('http://172.16.140.47/ecshop/')
    shops.login_ecshop()

    # # 登录方法调用
    # login = LoginPage(driver)
    # login.input_username('wuxn')
    # login.input_password('111111')
    # login.input_remember()
    # login.input_submit()
    #
    # # 窗口最大化
    # driver.maximize_window()
    #
    shops.find_shop()  # 立即购买
    # shops.buy()  # 结算按钮
    # # shops.update_shop()     # 更新商品数量并且结算
    # shops.info_person()     # 收货人信息
    # shops.delivery_way()    # 选择配送方式并且提交订单
    # # shops.delete_shop()     # 删除商品
    # # shops.delete_car()      # 清空购物车
    shops.goto_buy()  # 继续购物
    # # shops.car_shop()          # 进入购物车
